package cn.iocoder.yudao.module.bpm.controller.admin.oaleavapply;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.bpm.controller.admin.oaleavapply.vo.*;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oaleavapply.OaLeavApplyDO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oaleavapplysub.OaLeavApplySubDO;
import cn.iocoder.yudao.module.bpm.service.oaleavapply.OaLeavApplyService;

@Tag(name = "管理后台 - 申请单")
@RestController
@RequestMapping("/bpm/oa-leav-apply")
@Validated
public class OaLeavApplyController {

    @Resource
    private OaLeavApplyService oaLeavApplyService;

    @PostMapping("/create")
    @Operation(summary = "创建申请单")
    public CommonResult<Long> createOaLeavApply(@Valid @RequestBody OaLeavApplySaveReqVO createReqVO) {
        return success(oaLeavApplyService.createOaLeavApply(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新申请单")
    public CommonResult<Boolean> updateOaLeavApply(@Valid @RequestBody OaLeavApplySaveReqVO updateReqVO) {
        oaLeavApplyService.updateOaLeavApply(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除申请单")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteOaLeavApply(@RequestParam("id") Long id) {
        oaLeavApplyService.deleteOaLeavApply(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得申请单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<OaLeavApplyRespVO> getOaLeavApply(@RequestParam("id") Long id) {
        OaLeavApplyDO oaLeavApply = oaLeavApplyService.getOaLeavApply(id);
        return success(BeanUtils.toBean(oaLeavApply, OaLeavApplyRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得申请单分页")
    public CommonResult<PageResult<OaLeavApplyRespVO>> getOaLeavApplyPage(@Valid OaLeavApplyPageReqVO pageReqVO) {
        PageResult<OaLeavApplyDO> pageResult = oaLeavApplyService.getOaLeavApplyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OaLeavApplyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出申请单 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportOaLeavApplyExcel(@Valid OaLeavApplyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<OaLeavApplyDO> list = oaLeavApplyService.getOaLeavApplyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "申请单.xls", "数据", OaLeavApplyRespVO.class,
                        BeanUtils.toBean(list, OaLeavApplyRespVO.class));
    }

    // ==================== 子表（请假申请） ====================

    @GetMapping("/oa-leav-apply-sub/list-by-fk")
    @Operation(summary = "获得请假申请列表")
    @Parameter(name = "fk", description = "外键")
    public CommonResult<List<OaLeavApplySubDO>> getOaLeavApplySubListByFk(@RequestParam("fk") Long fk) {
        return success(oaLeavApplyService.getOaLeavApplySubListByFk(fk));
    }

}
